//
//  ZDnlTfJLCFMineWithdrawConfirmControllerftiUGb.m
//  Srb_ios
//
//  Created by Ji on 2018/9/18.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "ZDnlTfJLCFMineWithdrawConfirmControllerftiUGb.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"

@interface ZDnlTfJLCFMineWithdrawConfirmControllerftiUGb ()<UITextFieldDelegate>

@property (nonatomic, strong) bpXPwZJLCFMineSettingDataModelhmSxqV *data;

@property (nonatomic, strong) UITextField *aSHWXpmsgTextFieldgHtMSl;
@property (nonatomic, strong) UIButton *msgBtn;

@property (nonatomic, strong) NSTimer *qdZZsdcountTimereVpqYS;
@property (nonatomic, assign) int count;

@end

static int const showtime = 60;


@implementation ZDnlTfJLCFMineWithdrawConfirmControllerftiUGb

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addNavigationBar];
    self.navigationBar.title = @"账户提现";
    self.view.backgroundColor = QTZ_Gray237;
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    
    [sHkPtEJLCFMineRequestozAYZs settingWithParams:nil loadingInView:self.view success:^(bpXPwZJLCFMineSettingDataModelhmSxqV *data) {
        
        self.data = data;
        [self configView];
        
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
    
}

- (void)configView
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 13 + NAVBAR_HEIGHT, FULL_WIDTH, 120)];
    bgView.backgroundColor = QTZ_White;
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    
    UILabel *bankLabel = [UILabel labelWithText:@"银行卡号" textColor:QTZ_Gray51 font:FONT(16)];
    bankLabel.left = 11;
    bankLabel.centerY = 20;
    [bgView addSubview:bankLabel];
    
    UILabel *bankCardLabel = [UILabel labelWithText:self.data.bankCardNo textColor:QTZ_Gray153 font:FONT(16)];
    bankCardLabel.left = 106;
    bankCardLabel.centerY = bankLabel.centerY;
    [bgView addSubview:bankCardLabel];
    [bankLabel.layer addSublayer:[CALayer addLineOriginPoint:CGPointMake(0, bankLabel.height + 12) toPoint:CGPointMake(bgView.width - 22, bankLabel.height + 12) color:QTZ_Gray237 borderWidth:.5]];
                                                                                                                       
    UILabel *jyvhKwphoneLabelpopXEs = [UILabel labelWithText:@"预留手机号" textColor:QTZ_Gray51 font:FONT(16)];
    jyvhKwphoneLabelpopXEs.left = bankLabel.left;
    jyvhKwphoneLabelpopXEs.centerY = 60;
    [bgView addSubview:jyvhKwphoneLabelpopXEs];
    
    UILabel *UabOWQphoneNumberaYOEQNLabel = [UILabel labelWithText:[NSString replaceStringWithAsterisk:[JLCFRegisterService shareService].phone startLocation:3 lenght:4] textColor:QTZ_Gray153 font:FONT(16)];
    UabOWQphoneNumberaYOEQNLabel.left = 106;
    UabOWQphoneNumberaYOEQNLabel.centerY = jyvhKwphoneLabelpopXEs.centerY;
    [bgView addSubview:UabOWQphoneNumberaYOEQNLabel];
    
    UILabel *msgLabel = [UILabel labelWithText:@"验证码" textColor:QTZ_Gray51 font:FONT(16)];
    msgLabel.left = 11;
    msgLabel.centerY = 100;
    [bgView addSubview:msgLabel];
    
    self.aSHWXpmsgTextFieldgHtMSl = [[UITextField alloc] initWithFrame:CGRectMake(106, 0, FULL_WIDTH - 106 - 11, 30)];
    self.aSHWXpmsgTextFieldgHtMSl.centerY = msgLabel.centerY;
    self.aSHWXpmsgTextFieldgHtMSl.textAlignment = NSTextAlignmentLeft;
    self.aSHWXpmsgTextFieldgHtMSl.font = FONT(16);
    self.aSHWXpmsgTextFieldgHtMSl.delegate = self;
    self.aSHWXpmsgTextFieldgHtMSl.placeholder = @"请输入您的验证码";
    self.aSHWXpmsgTextFieldgHtMSl.keyboardType = UIKeyboardTypeNumberPad;
    self.aSHWXpmsgTextFieldgHtMSl.returnKeyType = UIReturnKeyDone;
    self.aSHWXpmsgTextFieldgHtMSl.clearButtonMode = UITextFieldViewModeWhileEditing;
    [bgView addSubview:self.aSHWXpmsgTextFieldgHtMSl];
    
    self.msgBtn = [UIButton new];
    self.msgBtn.titleLabel.font = FONT(9);
    [self.msgBtn setTitleColor:Theme_Color forState:0];
    [self.msgBtn setTitle:@"发送验证码" forState:0];
    [self.msgBtn sizeToFit];
    [self.msgBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    self.aSHWXpmsgTextFieldgHtMSl.rightView = self.msgBtn;
    self.aSHWXpmsgTextFieldgHtMSl.rightViewMode = UITextFieldViewModeAlways;
 
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(38, FULL_HEIGHT - 67, FULL_WIDTH - 76, 40)];
    [confirmBtn setBackgroundColor:Theme_Color];
    confirmBtn.titleLabel.font = FONT(15);
    [confirmBtn setTitle:@"完成" forState:0];
    [confirmBtn setTitleColor:QTZ_White forState:0];
    confirmBtn.layer.cornerRadius = 5;
    [confirmBtn addTarget:self action:@selector(handleConfirm) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
    
}

- (void)handleConfirm
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:self.amount forKey:@"amt"];
    [params setSafeObject:self.aSHWXpmsgTextFieldgHtMSl.text forKey:@"code"];
    [params setSafeObject:CHANNEL forKey:@"channel"];
    
    [sHkPtEJLCFMineRequestozAYZs withdrawWith:params loadingInView:self.view success:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:MINE_REFRESH object:nil];
        [self showNotice:nil image:[UIImage imageNamed:@"icon_withdraw_success"] afterDelay:1.5f];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[JinLiCaiFuApplicationEntrace shareEntrance].currentNavController popToRootViewControllerAnimated:YES];
        });
    } failure:^(NSString *error) {
        [self showNotice:error image:[UIImage imageNamed:@"icon_withdraw_failure"] afterDelay:1.5f];
    }];
    
}

//获取验证码
- (void)getVerifyCode
{
    [sHkPtEJLCFMineRequestozAYZs codeBankWithParams:@{@"type" : @"7"} loadingInView:nil success:^{
        [self handleVerifyBtn];
        [self showNotice:@"验证码发送成功"];
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
    
}

- (void)handleVerifyBtn
{
    _count = showtime;
    [[NSRunLoop mainRunLoop] addTimer:self.qdZZsdcountTimereVpqYS forMode:NSRunLoopCommonModes];
}

- (NSTimer *)qdZZsdcountTimereVpqYS
{
    if (!_qdZZsdcountTimereVpqYS) {
        _qdZZsdcountTimereVpqYS = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
    return _qdZZsdcountTimereVpqYS;
}

- (void)countDown
{
    _count --;
    [self.msgBtn setTitle:[NSString stringWithFormat:@"倒计时%ds",_count] forState:UIControlStateNormal];
    [self.msgBtn setTitleColor:QTZ_Gray173 forState:0];
    self.msgBtn.userInteractionEnabled = NO;
    if (_count == 0) {
        [self.msgBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.msgBtn setTitleColor:Theme_Color forState:0];
        self.msgBtn.userInteractionEnabled = YES;
        [self dismiss];
    }
    [self.msgBtn sizeToFit];
}

- (void)dismiss
{
    [self.qdZZsdcountTimereVpqYS invalidate];
    self.qdZZsdcountTimereVpqYS = nil;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

@end
